package leecode

func findAndReplacePattern(words []string, pattern string) []string {
	var result []string
	for i := 0; i < len(words); i++ {
		if isMatch(words[i], pattern) && isMatch(pattern, words[i]) {
			result = append(result, words[i])
		}
	}
	return result
}

func isMatch(word, pattern string) bool {
	hashMap := make(map[rune]byte)
	for i, x := range word {
		temp := pattern[i]
		if _, ok := hashMap[x]; !ok {
			hashMap[x] = temp
		} else if hashMap[x] != temp {
			return false
		}
	}
	return true
}
